package com.r2dbc.controller;

import com.r2dbc.common.Result;
import com.r2dbc.domain.User;
import com.r2dbc.domain.UserDetail;
import com.r2dbc.repository.UserRepository;
import com.r2dbc.repository.impl.MyUserRepository;
import com.r2dbc.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/*
 *@Description<p></p>
 *@Author gz
 *@CreateTime 2025/8/12 9:47
 *@CopyRight @2021-2025 卓望信息技术（北京）公司
 */

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MyUserRepository myUserRepository;
    @Autowired
    private UserRepository userRepository;

    @PostMapping("/save")
    public Mono<Result<Object>> save(@RequestBody User user) {
        Mono<User> save = userService.save(user);
        return Result.ok(save.flatMap(user1 -> Mono.just(user1.getId())));
    }


    //@GetMapping("/findById/{id}")
    public Mono<Result<User>> findById(@PathVariable Long id) {
        //int i = 1 / 0;  // 测试异常处理
        return Result.ok(userService.findById(id));
    }

    @GetMapping("/findById/{id}")
    public ResponseEntity<Mono<User>> findById1(@PathVariable Long id) {
        //int i = 1 / 0;  // 测试异常处理
        return ResponseEntity.ok(userService.findById(id));
    }

    @GetMapping("/findAll")
    public ResponseEntity<Flux<User>> findAll() {
        return ResponseEntity.ok(userService.findAll().flatMap(user -> Flux.just(user)));
    }


    @GetMapping("/dbClient/{id}")
    public ResponseEntity<Flux<User>> findAll1(@PathVariable Long id) {
        return ResponseEntity.ok(myUserRepository.dbClientQuery(id));
    }

    @GetMapping("/dbClientAll")
    public ResponseEntity<Flux<UserDetail>> findAll2() {
        return ResponseEntity.ok(myUserRepository.dbClientQueryAll());
    }

    @GetMapping("/query/{id}")
    public ResponseEntity<Mono<User>> findAll3(@PathVariable Long id) {
        return ResponseEntity.ok(userRepository.queryUser(id));
    }

}
